import { Module } from 'vuex'
import { rightsModuleTypeInterface, GlobalDataProps, RolesTypeInterface } from '@/store/storeType'
import { getFn, postFn, putFn } from '@/store/store_actions_axiosFn'
import { arrToObj } from '@/utils/helper'
import { deleteFun } from '@/utils/axiosFunc'

const updateDataFunc = (state: rightsModuleTypeInterface, res: any) => {
  state.rolesData[res.data.data.roleId] = res.data.data
  state.tagsData[res.data.data.roleId] = JSON.parse(JSON.stringify(res.data.data))
  state.tagsData[res.data.data.roleId].disabled = false
}
const initGetRolesData = <T extends { rolesData: { [key: number]: RolesTypeInterface }, tagsData: { [key: number]: RolesTypeInterface } }, E extends { [key: number]: { disabled?: boolean } }>(state: T, arrToObj: E ) => {
  const objData = arrToObj
  for (const key in objData) {
    objData[key].disabled = false
  }
  state.rolesData = arrToObj
  state.tagsData = JSON.parse(JSON.stringify(objData))
}

const deleteFunc = (state: rightsModuleTypeInterface, roleId: number) => {
  delete state.rolesData[roleId]
  delete state.tagsData[roleId]
}

const rightsModule: Module<rightsModuleTypeInterface, GlobalDataProps> = {
  namespaced: true,
  state: () => ({
    rolesData: {},
    treeData: [],
    tagsData: {},
    rightsList: []
  }),
  mutations: {
    setDisabled (state, ids) {
      state.tagsData[ids].disabled = !state.tagsData[ids].disabled
    },
    getRolesData (state, res) {
      if (res?.data?.meta?.status === 200) {
        initGetRolesData(state, arrToObj(res.data.data))
      }
    },
    postAddRoles (state, res) {
      if (res?.data?.meta?.status === 201) {
        updateDataFunc(state, res)
      }
    },
    putRoles (state, res) {
      if (res?.data?.meta?.status === 200) {
        updateDataFunc(state, res)
      }
    },
    deleteRoles (state, res) {
      deleteFunc(state, res.roleId)
    },
    getTerrData (state, res) {
      if (res?.data?.meta?.status === 200) {
        state.treeData = res.data.data
      }
    },
    postChangeRights (state, res) {
      console.log(state, '-----', res)
    },
    deleteRolesRights (state, resAndId) {
      const { resData, idsObj } = resAndId
      state.tagsData[idsObj.rolesId].children = resData.data.data
    },
    getRightsList (state, res) {
      if (res?.data?.meta?.status === 200) {
        state.rightsList = res.data.data
      }
    }
  },
  actions: {
    getRolesData ({ commit }) {
      return getFn('roles', 'getRolesData', commit)
    },
    postAddRoles ({ commit }, postData) {
      return postFn('roles', postData, 'postAddRoles', commit)
    },
    putRoles ({ commit }, putDataAndId) {
      return putFn(`roles/${putDataAndId.roleId}`, putDataAndId, 'putRoles', commit)
    },
    async deleteRoles ({ commit }, roleId) {
      const res = await deleteFun(`roles/${roleId.roleId}`)
      commit('deleteRoles', {res, roleId: roleId.roleId})

      return res
    },
    getTerrData ({ commit }) {
      return getFn('rights/tree', 'getTerrData', commit)
    },
    postChangeRights({ commit }, ridsPostDataAndRoleId) {
      return postFn(`roles/${ridsPostDataAndRoleId.roleId}/rights`, ridsPostDataAndRoleId.data, 'postChangeRights', commit)
    },
    async deleteRolesRights ({ commit }, idsObj) {
      const resData = await deleteFun(`roles/${idsObj.rolesId}/rights/${idsObj.rightsId}`)
      commit('deleteRolesRights', { resData, idsObj })
      return resData
    },
    getRightsList ({ commit }) {
      return getFn('rights/list', 'getRightsList', commit)
    }
  },
  getters: {
    rolesDataGetters: (state) => (roleId: number) => {
      return state.rolesData[roleId]
    }
  }
}

export default rightsModule